package com.pzh.guli.service.statics.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pzh.guli.service.base.result.Result;
import com.pzh.guli.service.statics.feign.CenterMemberService;
import com.pzh.guli.service.statics.pojo.Daily;
import com.pzh.guli.service.statics.mapper.DailyMapper;
import com.pzh.guli.service.statics.service.IDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzh
 * @since 2022-07-23
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements IDailyService {
    @Resource
    private DailyMapper dailyMapper;
    @Resource
    private CenterMemberService centerMemberService;
    //根据每天生成统计数据
    @Override
    public void createStaticsByDay(String day) {
        Result result = centerMemberService.countMemberNumByDay(day);
        Map<String, Object> item = result.getData();
        Integer registerNum = (Integer) item.get("item");
        //生成三个模拟数据
        Random random=new Random();
        Integer loginNum=random.nextInt(200);
        Integer viewNum=random.nextInt(200);
        Integer courseAdd=random.nextInt(200);
        Daily daily=new Daily();
        daily.setDateCalculated(day);
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(viewNum);
        daily.setCourseAddNum(courseAdd);
        dailyMapper.insert(daily);
    }
    //生成echarts数据
    @Override
    public Map<String, Map<String, Object>> showEchartsBytime(String begin, String end) {
        Map<String, Object> register_num = this.getEchartsDataByType(begin, end, "register_num");
        Map<String, Object> login_num = this.getEchartsDataByType(begin, end, "login_num");
        Map<String, Object> video_view_num = this.getEchartsDataByType(begin, end, "video_view_num");
        Map<String, Object> course_add_num = this.getEchartsDataByType(begin, end, "course_add_num");
        Map<String, Map<String, Object>> mapHashMap = new HashMap<>();
        mapHashMap.put("register_num",register_num);
        mapHashMap.put("login_num",login_num);
        mapHashMap.put("video_view_num",video_view_num);
        mapHashMap.put("course_add_num",course_add_num);
        return mapHashMap;
    }
    //组装数据工具
    private Map<String,Object> getEchartsDataByType(String begin,String end,String type){
        Map<String,Object> map=new HashMap<>();
        List<String> xList=new ArrayList<>();
        List<Integer> yList=new ArrayList<>();
        QueryWrapper<Daily> dailyQueryWrapper = new QueryWrapper<>();
        dailyQueryWrapper.select("date_calculated",type);
        dailyQueryWrapper.between("date_calculated",begin,end);
        List<Map<String, Object>> maps = dailyMapper.selectMaps(dailyQueryWrapper);
        for (Map<String, Object> stringObjectMap : maps) {
            String date_calculated = (String)stringObjectMap.get("date_calculated");
            xList.add(date_calculated);
            Integer num = (Integer)stringObjectMap.get(type);
            yList.add(num);
        }
        map.put("xData",xList);
        map.put("yData",yList);
        return map;
    }
}
